/**
 * Created by LuBangTao on 2017/3/27.
 */
var fireworkVersion5 = function (scene) {
    $(".cut-show").css("max-width", $(window).width());
    $(".cut-show").css("max-height", $(window).height());
    var r = 255;
    var g = 192;
    var b = 203;
    var alpha = 1;
    var color = new BABYLON.Color4(r/255, g/255, b/255, alpha);
    var myPoints = [
        new BABYLON.Vector3(0, 0, 0),
        new BABYLON.Vector3(screenLength, 0, 0),
        new BABYLON.Vector3(screenLength, 0, screenWidth),
        new BABYLON.Vector3(0, 0, screenWidth),
        new BABYLON.Vector3(0, 0, 0),
        new BABYLON.Vector3(0, screenHeight, 0),
        new BABYLON.Vector3(screenLength, screenHeight, 0),
        new BABYLON.Vector3(screenLength, screenHeight, screenWidth),
        new BABYLON.Vector3(0, screenHeight, screenWidth),
        new BABYLON.Vector3(0, screenHeight, 0),
        new BABYLON.Vector3(0, 0, 0),
        new BABYLON.Vector3(0, 0, screenWidth),
        new BABYLON.Vector3(0, screenHeight, screenWidth),
        new BABYLON.Vector3(screenLength, screenHeight, screenWidth),
        new BABYLON.Vector3(screenLength, 0, screenWidth),
        new BABYLON.Vector3(screenLength, 0, 0),
        new BABYLON.Vector3(screenLength, screenHeight, 0),
    ];
    secondCanvas.width = screenLength * screenWidth;
    secondCanvas.height = screenHeight;
    currentFireCount++;
    var thisFireCount = currentFireCount;
    var lines = BABYLON.MeshBuilder.CreateLines(thisFireCount + "lines", {points: myPoints, updatable: true}, scene);
    if (countCapacity < maxCapacity) {
        countCapacity++;
    } else {
        Lighting.sendMessageToServer(0);
    }
    var startSphere = new BABYLON.Mesh.CreateSphere(thisFireCount + "Shoot", 4, 1, scene);
    startSphere.position.x = screenLength / 2;
    startSphere.position.z = screenWidth / 2;
    startSphere.isVisible = false;
    var particleSystem = new BABYLON.ParticleSystem("particles", 20, scene);
    particleSystem.particleTexture = new BABYLON.Texture("texture/flare.png", scene);
    particleSystem.emitter = startSphere; // the starting object, the emitter
    particleSystem.minEmitBox = new BABYLON.Vector3(0, 0, 0); // Starting all from
    particleSystem.maxEmitBox = new BABYLON.Vector3(0, 0, 0); // To...
    particleSystem.color1 = color;
    particleSystem.color2 = color;
    particleSystem.colorDead = new BABYLON.Color4(0, 0, 0, 0);
    particleSystem.minSize = .1;
    particleSystem.maxSize = .1;
    particleSystem.minLifeTime = .25;
    particleSystem.maxLifeTime = .25;
    particleSystem.emitRate = 50;
    particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
    particleSystem.gravity = new BABYLON.Vector3(0, -9.81, 0);
    // particleSystem.direction1 = new BABYLON.Vector3(0, -3, 0);
    // particleSystem.direction2 = new BABYLON.Vector3(0, -3, 0);
    particleSystem.minEmitPower = 1;
    particleSystem.maxEmitPower = 1;
    particleSystem.updateSpeed = 0.005;
    var updateFunction = function (particles) {
        for (var index = 0; index < particles.length; index++) {
            var particle = particles[index];
            particle.age += this._scaledUpdateSpeed;
            if (particle.age >= particle.lifeTime) {
                this.recycleParticle(particle);
                index--;
                continue;
            }
            else {
                Lighting.drawFireworkData(particle.position.x, particle.position.y, particle.position.z, screenHeight, screenLength, secondCanvas.getContext("2d"),particle.color);
                particle.direction.scaleToRef(particleSystem._scaledUpdateSpeed, particleSystem._scaledDirection);
                particle.position.addInPlace(particleSystem._scaledDirection);
                particleSystem.gravity.scaleToRef(particleSystem._scaledUpdateSpeed, particleSystem._scaledGravity);
                particle.direction.addInPlace(particleSystem._scaledGravity);
            }
        }
    };
    particleSystem.updateFunction = updateFunction;
    particleSystem.start();
    var isTop1 = false;
    var isFired1 = false;
    scene.registerBeforeRender(function () {
        secondCanvas.width = secondCanvas.width;
        if (!isFired1) {
            if (!isTop1) {
                startSphere.position.y += .1;
                if (startSphere.position.y > screenHeight * 2 / 3) {
                    isTop1 = !isTop1;
                    particleSystem.stop();
                }
            } else {
                setTimeout(function () {
                    fire(scene);
                }, 1000);
                isFired1 = !isFired1;
            }
        }
    });
    function fire(scene) {
        var tempFire = [];
        var tempGizmo = [];
        var fountain = new BABYLON.Mesh.CreateSphere(thisFireCount + "explosion", 4, 1, scene);
        fountain.isVisible = false;
        fountain.position.x = Math.random()*minValue+(screenLength-minValue)/2;
        fountain.position.y = screenHeight * 2 / 3 + Math.random() * screenHeight / 4;
        fountain.position.z = Math.random()*minValue+(screenWidth-minValue)/2;
        var perticleFromVerticesEmitter = fountain;
        perticleFromVerticesEmitter.useVertexColors = true;
        var verticesPositions = perticleFromVerticesEmitter.getVerticesData(BABYLON.VertexBuffer.PositionKind);
        var verticesNormals = perticleFromVerticesEmitter.getVerticesData(BABYLON.VertexBuffer.NormalKind);
        var verticesColor = [];
        for (var i = 0; i < verticesPositions.length; i += 3) {
            var vertexPosition = new BABYLON.Vector3(
                verticesPositions[i],
                verticesPositions[i + 1],
                verticesPositions[i + 2]
            );
            var vertexNormal = new BABYLON.Vector3(
                verticesNormals[i],
                verticesNormals[i + 1],
                verticesNormals[i + 2]
            );
            verticesColor.push(r/255);
            verticesColor.push(g/255);
            verticesColor.push(b/255);
            verticesColor.push(alpha);
            var gizmo = BABYLON.Mesh.CreateBox(thisFireCount + 'gizmo', 0.001, scene);
            tempGizmo.push(gizmo);
            gizmo.position = vertexPosition;
            gizmo.parent = perticleFromVerticesEmitter;
            var tempP = createParticleSystem(
                gizmo,
                vertexNormal.normalize().scale(1),
                color
            );
            tempFire.push(tempP);
        }
        perticleFromVerticesEmitter.setVerticesData(BABYLON.VertexBuffer.ColorKind, verticesColor);
        var isDone1 = false;
        var isDispose1 = false;
        var isSend1 = false;

        function createParticleSystem(emitter, direction, color) {
            var particleSystem1 = new BABYLON.ParticleSystem(thisFireCount + "particles", 40, scene);
            var updateFunction = function (particles) {
                for (var index = 0; index < particles.length; index++) {
                    var particle = particles[index];
                    particle.age += this._scaledUpdateSpeed;
                    if (particle.age >= particle.lifeTime) {
                        this.recycleParticle(particle);
                        index--;
                        continue;
                    }
                    else {
                        Lighting.drawFireworkData(particle.position.x, particle.position.y, particle.position.z, screenHeight, screenLength, secondCanvas.getContext("2d"),particle.color)
                        particle.direction.scaleToRef(particleSystem1._scaledUpdateSpeed, particleSystem1._scaledDirection);
                        particle.position.addInPlace(particleSystem1._scaledDirection);
                        particleSystem1.gravity.scaleToRef(particleSystem1._scaledUpdateSpeed, particleSystem1._scaledGravity);
                        particle.direction.addInPlace(particleSystem1._scaledGravity);
                    }
                }
            };
            // var startPositionFunction = function (worldMatrix, positionToUpdate) {
            //     BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(this.domeRadius*(Math.random()*-10+Math.random()*10),this.domeRadius*(Math.random()*-10+Math.random()*10),this.domeRadius*(Math.random()*-10+Math.random()*10), worldMatrix, positionToUpdate);
            // };
            particleSystem1.updateFunction = updateFunction;
            particleSystem1.domeRadius = 1;
            particleSystem1.particleTexture = new BABYLON.Texture("texture/flare.png", scene);
            particleSystem1.emitter = emitter;
            particleSystem1.minEmitBox = new BABYLON.Vector3(0, 0, 0);
            particleSystem1.maxEmitBox = new BABYLON.Vector3(0, 0, 0);
            particleSystem1.color1 = color;
            particleSystem1.color2 = color;
            particleSystem1.colorDead = new BABYLON.Color4(0, 0, 0, 0.0);
            particleSystem1.minSize = .1;
            particleSystem1.maxSize = .1;
            particleSystem1.minLifeTime = .3 + Math.floor(screenHeight / 10) * .15;
            particleSystem1.maxLifeTime = particleSystem1.minLifeTime;
            particleSystem1.emitRate = 40;
            particleSystem1.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
            particleSystem1.gravity = new BABYLON.Vector3(0, -9, 0);
            particleSystem1.direction1 = direction;
            particleSystem1.direction2 = direction;
            particleSystem1.minEmitPower = 5 + Math.floor(screenHeight / 10) * 1.5;
            particleSystem1.maxEmitPower = particleSystem1.minEmitPower;
            particleSystem1.updateSpeed = 0.005;

            return particleSystem1;
        }

        var timer1 = 0;
        scene.registerBeforeRender(function () {
            if (!isSend1) {
                for (var i = 0; i < tempFire.length; i++) {
                    tempFire[i].start();
                }
                isSend1 = !isSend1;
            }
            if (!isDone1) {
                if (timer1 < screenHeight / 3) {
                    timer1 += 0.1
                } else {
                    for (var i = 0; i < tempFire.length; i++) {
                        tempFire[i].stop();
                    }
                    isDone1 = !isDone1;
                }
            } else {
                if (!isDispose1) {
                    timer1 += 0.1;
                    if (timer1 > screenHeight) {
                        startSphere.dispose();
                        fountain.dispose();
                        for(var i =0;i<tempGizmo.length;i++){
                            tempGizmo[i].dispose();
                        }
                        for (var i = 0; i < scene.meshes.length; i++) {
                            if (scene.meshes[i].id.indexOf(thisFireCount.toString()) >= 0) {
                                scene.meshes[i].dispose();
                            }
                        }
                        countCapacity--;
                        isDispose1 = !isDispose1;
                        timer1 = 0;
                    }
                }
            }
        });
    }
}
